﻿using nplJobsCommon.JobSeeker;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace nplJobsDAL.Select
{
    public class SelectSavedJobToDisplay: DataAccessBase
    {
        public List<SavedJob> SavedJob { get; set; }
        public Int64 SavedJobID { get; set; }
        public Guid JobseekerID { get; set; }

        public SelectSavedJobToDisplay()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectDisplaySavedJobs.ToString();
        }

        #region GetSavedJobToDisplay
        public List<SavedJobToDisplay> GetSavedJobToDisplay()
        {
            List<SavedJobToDisplay> lst = new List<SavedJobToDisplay>();
            DataBaseHelper dbHelper;
            DataSet dsSavedJobToDisplay;

            try
            {
                SelectSavedJobParameters selectSavedJobsParameters = new SelectSavedJobParameters(this.SavedJobID, this.JobseekerID);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selectSavedJobsParameters.Parameters;
                dsSavedJobToDisplay = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                lst = GetList(dsSavedJobToDisplay);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return lst;
        }
        #endregion

        #region SQL parameters for selecting applied info by id
        public class SelectSavedJobParameters
        {
            private SqlParameter[] _parameters;

            public SelectSavedJobParameters(Int64 appliedJobID, Guid jobseekerId)
            {
                this.SavedJobID = appliedJobID;
                this.JobseekerID = jobseekerId;
                Build();
            }

            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@SavedJobID", SavedJobID),
                    new SqlParameter("@JobseekerID", JobseekerID),
                };

                Parameters = parameters;
            }

            public Int64 SavedJobID { get; set; }
            public Guid JobseekerID { get; set; }


            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion

        #region GetList
        private List<SavedJobToDisplay> GetList(DataSet dsSavedJobToDisplay)
        {
            List<SavedJobToDisplay> savedJobToDisplay;
            try
            {
                savedJobToDisplay = (from n in dsSavedJobToDisplay.Tables[0].AsEnumerable()
                                select new SavedJobToDisplay
                                   {
                                       SavedJobID = n.Field<Int64>("ID"),
                                       JobID = n.Field<Int64>("JobID"),
                                       JobSeekerID = n.Field<Guid>("JobSeekerID"),
                                       CompanyImage = n.Field<string>("CompanyImage"),
                                       ApplicationDateline = n.Field<int>("ApplicationDateline"),
                                       JobTitle = n.Field<string>("JobTitle"),
                                       Location = n.Field<string>("Location"),
                                       CompanyName = n.Field<string>("CompanyName"),
                                       PostedDate = n.Field<DateTime>("PostedDate")
                                   }).ToList();

                return savedJobToDisplay;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion
    }
}
